<?php

use app\common\services\UrlServices;

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=750">
    <title>九洲口令，红包百应</title>
    <script type="text/javascript" src="<?=UrlServices::buildWWWUrl('/hongbao/js/vie.js')?>"></script>
    <link rel="stylesheet" href="<?=UrlServices::buildWWWUrl('/hongbao/css/common.css')?>">
    <link rel="stylesheet" href="<?=UrlServices::buildWWWUrl('/hongbao/css/event.css')?>">
</head>
<body>
<div class="container">
    <div class="chat">
        <div class="headImg"><img src="<?=UrlServices::buildWWWUrl('/hongbao/images/1.jpg')?>" alt=""></div>
        <div class="chat-container">
            <p class="name">九洲新世界广场</p>
            <div class="chat-content">本次共4871份红包，还剩<?=$prize_count['prize_count']?>份</div>
        </div>
    </div>
    <div class="say">
        <p class="prompt">这个春天和九洲新世界广场有个约“惠”</p>
        <div class="say-btn" id="say"></div>
        <p class="word">按住&nbsp;&nbsp;&nbsp;读口令</p>
    </div>
    <div class="foot">
        <a href="<?=UrlServices::buildMUrl('/activity/index')?>" class="tohome">首页</a>
        <a href="<?=UrlServices::buildMUrl('/activity/prize-list')?>" class="<?php if ($my_prize_count): ?>tomyprize<?php else: ?><?php endif;?>" data-num="<?=$my_prize_count?>" >我的奖品</a>
    </div>
</div>

<script src='<?=UrlServices::buildWWWUrl('/hongbao/js/jquery.min.js')?>'></script>
<script>
    $(document).ready(function () {
        $.ajax({
            url: '/weixin/activity/check-prize',
            type: 'GET',
            dataType: 'json',
            data: {},
            success: function (res) {
               if(res.code ==200){
                   $("#say").on('touchstart',function(){
                       weixin.share();
                   });
               }else{
                   alert('分享朋友圈能多一次抽奖');
               }
            }
        });
    });
</script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
    <?=$js_info?>
    var weixin = {
        share: function (option) {
            wx.ready(function () {
                Distinguish(function () {
                    $.ajax({
                        url: '/weixin/activity/get-prize',
                        type: 'POST',
                        dataType: 'json',
                        data: {
                            prize_token:'ee976f20c81815b2'
                        },
                        success: function (res) {
                            if (res.code == 200) {
                                window.location.href = '/weixin/activity/prize-result?is_ok=1';
                            }else{
                                alert(res.msg);
                            }
                        }
                    });
                });
            });
        }
    };
    //哈气
    function Distinguish(fc) {
        var num_action = 0;//哈气次数
        controlRecord(5000);

        function controlRecord(timer) {
            wx.startRecord({
                success: function (res) {
                    // alert("开始录音success");
                    var timerRecord = 0;
                    //clearTimeout(timerRecord);

                    timerRecord = setTimeout(function () {
                        scene_transIng();
                        wx.stopRecord({
                            success: function (res) {
                                var localId = res.localId;
                                clearTimeout(timerRecord);


                                wx.translateVoice({
                                    localId: localId, // 需要识别的音频的本地Id，由录音相关接口获得
                                    isShowProgressTips: 1, // 默认为1，显示进度提示
                                    success: function (res) {
                                        var str_tran = res.translateResult;
                                        if (getRecord(str_tran)) { //识别成功
                                            fc();
                                        } else {
                                            window.location.href = '/weixin/activity/prize-result?is_ok=0';
                                        }
                                        // alert('识别结果：' + res.translateResult);
                                        clearTimeout(timerRecord);
                                    },
                                    fail: function () { //识别失败

//                                    scene_transFail();
                                        clearTimeout(timerRecord);
                                        return;
                                    }
                                });
                            },
                            fail: function () {
//                            scene_transFail();
                                $('.wave').fadeOut();
                                clearTimeout(timerRecord);
                                return;
                            }
                        });
                    }, timer);
                },
                cancel: function () {
                    $('.wave').fadeOut();
                    // alert("用户取消录音");
                    // scene_transSucces()
                }
            });
        }

// 验证语音
        function getRecord(str) {
            if (str.indexOf('九洲') >-1 && str.indexOf('春天')>-1 && str.indexOf('有个约')>-1 && str.indexOf('这个')>-1) {
                return true;
            }
            return false
        }

//界面：识别中
        function scene_transIng() {
            console.log("识别中...");
        }

//界面：识别失败
        function scene_transFail() {
            num_action++;
            if (num_action <= 1) {
                setTimeout(function () {
                    controlRecord(5000)
                    // scene_transIng();
                    // setTimeout(scene_transSucces, 3000);
                }, 2000);
            } else {
                //scene_transIng();
                //setTimeout(scene_transSucces, 1000);
                //num_action = 0;
            }

            _hmt.push(['_trackEvent', '场景', '场景_哈气', '场景_哈气_识别失败']);
        }

//界面：识别成功
        function scene_transSucces() {
            if (fc)
                fc();
            wx.stopRecord();
        }
    }

</script>
<script>
    <?=$js_info?>
</script>
<script href="<?=UrlServices::buildWWWUrl('/js/common.js')?>"></script>
</body>
</html>
